from selenium.webdriver.common.by import By
from pages.base_page import Page


class ChatBotPage(Page):
    ele_title = (By.XPATH, u"//h3[text()='AI Chatbot']")
    ele_error = (By.XPATH, u'//div[@role="alert"]')

    ele_chat_bot_for_campaign_box = (By.ID, u'form-integrationChatBotConfig-select-selectedChatBot-inputLabel')
    ele_allocate_online = (By.XPATH, u"//input[@name='isChatbotAllocatedWhenAgentOnline']")
    ele_allocate_offline = (By.XPATH, u"//input[@name='isChatbotAllocatedWhenAgentOffline']")

    ele_is_enable_feature_switch = (By.XPATH, u"//input[@name='isEnabled']")

    ele_ai_chat_bot_save_button = (By.XPATH, u"(//button[@data-tag='formButton'])[1]")

    # 如果没有AIChatBot功能，则检查图片和unlock button
    ele_img = (By.XPATH, u'//img[@alt="Webhooks Introduction"]')
    ele_unlock_this_feature = (By.XPATH, u'//span[text()="Chat with Us"]')

    def __init__(self, driver, url, site_id):
        Page.__init__(self, driver, url)
        self.site_id = site_id
        self.url = url

    # this method should be called first
    def is_enable_feature_switch(self):
        fs = self.find_element(*self.ele_is_enable_feature_switch)
        if fs.get_attribute('value') == "false":
            self.click(self.ele_is_enable_feature_switch)
        else:
            pass

    def check_ai_chat_bot_page_when_has_chat_bot_feature(self):
        # self.find_element(*self.ele_title).is_enabled()
        self.find_element(*self.ele_is_enable_feature_switch).is_enabled()
        self.find_element(*self.ele_chat_bot_for_campaign_box).is_enabled()
        self.find_element(*self.ele_allocate_online).is_enabled()
        self.find_element(*self.ele_allocate_offline).is_enabled()

    def check_ai_chat_bot_save_button(self):
        self.click(self.ele_allocate_online)
        self.click(self.ele_allocate_offline)
        self.click(self.ele_ai_chat_bot_save_button)

    def check_ai_chat_bot_page_when_no_chat_bot_feature(self):
        # self.find_element(*self.ele_title).is_enabled()
        self.find_element(*self.ele_img).is_enabled()
        self.find_element(*self.ele_unlock_this_feature).is_enabled()
